/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package daos;

import businessObjects.Pauta;
import daos.exceptions.IllegalOrphanException;
import daos.exceptions.NonexistentEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import businessObjects.Cliente;
import businessObjects.Esquema;
import businessObjects.Estado;
import businessObjects.Formulario;
import java.util.ArrayList;
import java.util.Collection;
import businessObjects.Pago;

/**
 *
 * @author williamprado
 */
public class PautaDAO {

    public PautaDAO() {
        //emf = Persistence.createEntityManagerFactory("Adz-ejbPU");
    }
    //private EntityManagerFactory emf = null;

    /*public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }*/

    /*public void create(Pauta pauta) {
        if (pauta.getFormularioCollection() == null) {
            pauta.setFormularioCollection(new ArrayList<Formulario>());
        }
        if (pauta.getPagoCollection() == null) {
            pauta.setPagoCollection(new ArrayList<Pago>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cliente loginCliente = pauta.getLoginCliente();
            if (loginCliente != null) {
                loginCliente = em.getReference(loginCliente.getClass(), loginCliente.getLogin());
                pauta.setLoginCliente(loginCliente);
            }
            Esquema idEsquema = pauta.getIdEsquema();
            if (idEsquema != null) {
                idEsquema = em.getReference(idEsquema.getClass(), idEsquema.getIdEsquema());
                pauta.setIdEsquema(idEsquema);
            }
            Estado idEstado = pauta.getIdEstado();
            if (idEstado != null) {
                idEstado = em.getReference(idEstado.getClass(), idEstado.getIdEstado());
                pauta.setIdEstado(idEstado);
            }
            Collection<Formulario> attachedFormularioCollection = new ArrayList<Formulario>();
            for (Formulario formularioCollectionFormularioToAttach : pauta.getFormularioCollection()) {
                formularioCollectionFormularioToAttach = em.getReference(formularioCollectionFormularioToAttach.getClass(), formularioCollectionFormularioToAttach.getIdFormulario());
                attachedFormularioCollection.add(formularioCollectionFormularioToAttach);
            }
            pauta.setFormularioCollection(attachedFormularioCollection);
            Collection<Pago> attachedPagoCollection = new ArrayList<Pago>();
            for (Pago pagoCollectionPagoToAttach : pauta.getPagoCollection()) {
                pagoCollectionPagoToAttach = em.getReference(pagoCollectionPagoToAttach.getClass(), pagoCollectionPagoToAttach.getIdPago());
                attachedPagoCollection.add(pagoCollectionPagoToAttach);
            }
            pauta.setPagoCollection(attachedPagoCollection);
            em.persist(pauta);
            if (loginCliente != null) {
                loginCliente.getPautaCollection().add(pauta);
                loginCliente = em.merge(loginCliente);
            }
            if (idEsquema != null) {
                idEsquema.getPautaCollection().add(pauta);
                idEsquema = em.merge(idEsquema);
            }
            if (idEstado != null) {
                idEstado.getPautaCollection().add(pauta);
                idEstado = em.merge(idEstado);
            }
            for (Formulario formularioCollectionFormulario : pauta.getFormularioCollection()) {
                Pauta oldIdPautaOfFormularioCollectionFormulario = formularioCollectionFormulario.getIdPauta();
                formularioCollectionFormulario.setIdPauta(pauta);
                formularioCollectionFormulario = em.merge(formularioCollectionFormulario);
                if (oldIdPautaOfFormularioCollectionFormulario != null) {
                    oldIdPautaOfFormularioCollectionFormulario.getFormularioCollection().remove(formularioCollectionFormulario);
                    oldIdPautaOfFormularioCollectionFormulario = em.merge(oldIdPautaOfFormularioCollectionFormulario);
                }
            }
            for (Pago pagoCollectionPago : pauta.getPagoCollection()) {
                Pauta oldIdPautaOfPagoCollectionPago = pagoCollectionPago.getIdPauta();
                pagoCollectionPago.setIdPauta(pauta);
                pagoCollectionPago = em.merge(pagoCollectionPago);
                if (oldIdPautaOfPagoCollectionPago != null) {
                    oldIdPautaOfPagoCollectionPago.getPagoCollection().remove(pagoCollectionPago);
                    oldIdPautaOfPagoCollectionPago = em.merge(oldIdPautaOfPagoCollectionPago);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }*/

    public void create(Pauta pauta, EntityManager em) throws Exception {
        if (pauta.getFormularioCollection() == null) {
            pauta.setFormularioCollection(new ArrayList<Formulario>());
        }
        if (pauta.getPagoCollection() == null) {
            pauta.setPagoCollection(new ArrayList<Pago>());
        }
        //EntityManager em = null;
        try {
            //em = getEntityManager();
            //em.getTransaction().begin();
            Cliente loginCliente = pauta.getLoginCliente();
            if (loginCliente != null) {
                loginCliente = em.getReference(loginCliente.getClass(), loginCliente.getLogin());
                pauta.setLoginCliente(loginCliente);
            }
            Esquema idEsquema = pauta.getIdEsquema();
            if (idEsquema != null) {
                idEsquema = em.getReference(idEsquema.getClass(), idEsquema.getIdEsquema());
                pauta.setIdEsquema(idEsquema);
            }
            Estado idEstado = pauta.getIdEstado();
            if (idEstado != null) {
                idEstado = em.getReference(idEstado.getClass(), idEstado.getIdEstado());
                pauta.setIdEstado(idEstado);
            }
            Collection<Formulario> attachedFormularioCollection = new ArrayList<Formulario>();
            for (Formulario formularioCollectionFormularioToAttach : pauta.getFormularioCollection()) {
                formularioCollectionFormularioToAttach = em.getReference(formularioCollectionFormularioToAttach.getClass(), formularioCollectionFormularioToAttach.getIdFormulario());
                attachedFormularioCollection.add(formularioCollectionFormularioToAttach);
            }
            pauta.setFormularioCollection(attachedFormularioCollection);
            Collection<Pago> attachedPagoCollection = new ArrayList<Pago>();
            for (Pago pagoCollectionPagoToAttach : pauta.getPagoCollection()) {
                pagoCollectionPagoToAttach = em.getReference(pagoCollectionPagoToAttach.getClass(), pagoCollectionPagoToAttach.getIdPago());
                attachedPagoCollection.add(pagoCollectionPagoToAttach);
            }
            pauta.setPagoCollection(attachedPagoCollection);
            em.persist(pauta);
            if (loginCliente != null) {
                loginCliente.getPautaCollection().add(pauta);
                loginCliente = em.merge(loginCliente);
            }
            if (idEsquema != null) {
                idEsquema.getPautaCollection().add(pauta);
                idEsquema = em.merge(idEsquema);
            }
            if (idEstado != null) {
                idEstado.getPautaCollection().add(pauta);
                idEstado = em.merge(idEstado);
            }
            for (Formulario formularioCollectionFormulario : pauta.getFormularioCollection()) {
                Pauta oldIdPautaOfFormularioCollectionFormulario = formularioCollectionFormulario.getIdPauta();
                formularioCollectionFormulario.setIdPauta(pauta);
                formularioCollectionFormulario = em.merge(formularioCollectionFormulario);
                if (oldIdPautaOfFormularioCollectionFormulario != null) {
                    oldIdPautaOfFormularioCollectionFormulario.getFormularioCollection().remove(formularioCollectionFormulario);
                    oldIdPautaOfFormularioCollectionFormulario = em.merge(oldIdPautaOfFormularioCollectionFormulario);
                }
            }
            for (Pago pagoCollectionPago : pauta.getPagoCollection()) {
                Pauta oldIdPautaOfPagoCollectionPago = pagoCollectionPago.getIdPauta();
                pagoCollectionPago.setIdPauta(pauta);
                pagoCollectionPago = em.merge(pagoCollectionPago);
                if (oldIdPautaOfPagoCollectionPago != null) {
                    oldIdPautaOfPagoCollectionPago.getPagoCollection().remove(pagoCollectionPago);
                    oldIdPautaOfPagoCollectionPago = em.merge(oldIdPautaOfPagoCollectionPago);
                }
            }
            //em.getTransaction().commit();
        } catch(Exception e) {
            throw e;
        }
    }

    /*public void edit(Pauta pauta) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Pauta persistentPauta = em.find(Pauta.class, pauta.getIdPauta());
            Cliente loginClienteOld = persistentPauta.getLoginCliente();
            Cliente loginClienteNew = pauta.getLoginCliente();
            Esquema idEsquemaOld = persistentPauta.getIdEsquema();
            Esquema idEsquemaNew = pauta.getIdEsquema();
            Estado idEstadoOld = persistentPauta.getIdEstado();
            Estado idEstadoNew = pauta.getIdEstado();
            Collection<Formulario> formularioCollectionOld = persistentPauta.getFormularioCollection();
            Collection<Formulario> formularioCollectionNew = pauta.getFormularioCollection();
            Collection<Pago> pagoCollectionOld = persistentPauta.getPagoCollection();
            Collection<Pago> pagoCollectionNew = pauta.getPagoCollection();
            List<String> illegalOrphanMessages = null;
            for (Formulario formularioCollectionOldFormulario : formularioCollectionOld) {
                if (!formularioCollectionNew.contains(formularioCollectionOldFormulario)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Formulario " + formularioCollectionOldFormulario + " since its idPauta field is not nullable.");
                }
            }
            for (Pago pagoCollectionOldPago : pagoCollectionOld) {
                if (!pagoCollectionNew.contains(pagoCollectionOldPago)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Pago " + pagoCollectionOldPago + " since its idPauta field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (loginClienteNew != null) {
                loginClienteNew = em.getReference(loginClienteNew.getClass(), loginClienteNew.getLogin());
                pauta.setLoginCliente(loginClienteNew);
            }
            if (idEsquemaNew != null) {
                idEsquemaNew = em.getReference(idEsquemaNew.getClass(), idEsquemaNew.getIdEsquema());
                pauta.setIdEsquema(idEsquemaNew);
            }
            if (idEstadoNew != null) {
                idEstadoNew = em.getReference(idEstadoNew.getClass(), idEstadoNew.getIdEstado());
                pauta.setIdEstado(idEstadoNew);
            }
            Collection<Formulario> attachedFormularioCollectionNew = new ArrayList<Formulario>();
            for (Formulario formularioCollectionNewFormularioToAttach : formularioCollectionNew) {
                formularioCollectionNewFormularioToAttach = em.getReference(formularioCollectionNewFormularioToAttach.getClass(), formularioCollectionNewFormularioToAttach.getIdFormulario());
                attachedFormularioCollectionNew.add(formularioCollectionNewFormularioToAttach);
            }
            formularioCollectionNew = attachedFormularioCollectionNew;
            pauta.setFormularioCollection(formularioCollectionNew);
            Collection<Pago> attachedPagoCollectionNew = new ArrayList<Pago>();
            for (Pago pagoCollectionNewPagoToAttach : pagoCollectionNew) {
                pagoCollectionNewPagoToAttach = em.getReference(pagoCollectionNewPagoToAttach.getClass(), pagoCollectionNewPagoToAttach.getIdPago());
                attachedPagoCollectionNew.add(pagoCollectionNewPagoToAttach);
            }
            pagoCollectionNew = attachedPagoCollectionNew;
            pauta.setPagoCollection(pagoCollectionNew);
            pauta = em.merge(pauta);
            if (loginClienteOld != null && !loginClienteOld.equals(loginClienteNew)) {
                loginClienteOld.getPautaCollection().remove(pauta);
                loginClienteOld = em.merge(loginClienteOld);
            }
            if (loginClienteNew != null && !loginClienteNew.equals(loginClienteOld)) {
                loginClienteNew.getPautaCollection().add(pauta);
                loginClienteNew = em.merge(loginClienteNew);
            }
            if (idEsquemaOld != null && !idEsquemaOld.equals(idEsquemaNew)) {
                idEsquemaOld.getPautaCollection().remove(pauta);
                idEsquemaOld = em.merge(idEsquemaOld);
            }
            if (idEsquemaNew != null && !idEsquemaNew.equals(idEsquemaOld)) {
                idEsquemaNew.getPautaCollection().add(pauta);
                idEsquemaNew = em.merge(idEsquemaNew);
            }
            if (idEstadoOld != null && !idEstadoOld.equals(idEstadoNew)) {
                idEstadoOld.getPautaCollection().remove(pauta);
                idEstadoOld = em.merge(idEstadoOld);
            }
            if (idEstadoNew != null && !idEstadoNew.equals(idEstadoOld)) {
                idEstadoNew.getPautaCollection().add(pauta);
                idEstadoNew = em.merge(idEstadoNew);
            }
            for (Formulario formularioCollectionNewFormulario : formularioCollectionNew) {
                if (!formularioCollectionOld.contains(formularioCollectionNewFormulario)) {
                    Pauta oldIdPautaOfFormularioCollectionNewFormulario = formularioCollectionNewFormulario.getIdPauta();
                    formularioCollectionNewFormulario.setIdPauta(pauta);
                    formularioCollectionNewFormulario = em.merge(formularioCollectionNewFormulario);
                    if (oldIdPautaOfFormularioCollectionNewFormulario != null && !oldIdPautaOfFormularioCollectionNewFormulario.equals(pauta)) {
                        oldIdPautaOfFormularioCollectionNewFormulario.getFormularioCollection().remove(formularioCollectionNewFormulario);
                        oldIdPautaOfFormularioCollectionNewFormulario = em.merge(oldIdPautaOfFormularioCollectionNewFormulario);
                    }
                }
            }
            for (Pago pagoCollectionNewPago : pagoCollectionNew) {
                if (!pagoCollectionOld.contains(pagoCollectionNewPago)) {
                    Pauta oldIdPautaOfPagoCollectionNewPago = pagoCollectionNewPago.getIdPauta();
                    pagoCollectionNewPago.setIdPauta(pauta);
                    pagoCollectionNewPago = em.merge(pagoCollectionNewPago);
                    if (oldIdPautaOfPagoCollectionNewPago != null && !oldIdPautaOfPagoCollectionNewPago.equals(pauta)) {
                        oldIdPautaOfPagoCollectionNewPago.getPagoCollection().remove(pagoCollectionNewPago);
                        oldIdPautaOfPagoCollectionNewPago = em.merge(oldIdPautaOfPagoCollectionNewPago);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = pauta.getIdPauta();
                if (findPauta(id) == null) {
                    throw new NonexistentEntityException("The pauta with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }*/

    public void edit(Pauta pauta, EntityManager em) throws IllegalOrphanException, NonexistentEntityException, Exception {
        //EntityManager em = null;
        try {
            //em = getEntityManager();
            //em.getTransaction().begin();
            Pauta persistentPauta = em.find(Pauta.class, pauta.getIdPauta());
            Cliente loginClienteOld = persistentPauta.getLoginCliente();
            Cliente loginClienteNew = pauta.getLoginCliente();
            Esquema idEsquemaOld = persistentPauta.getIdEsquema();
            Esquema idEsquemaNew = pauta.getIdEsquema();
            Estado idEstadoOld = persistentPauta.getIdEstado();
            Estado idEstadoNew = pauta.getIdEstado();
            Collection<Formulario> formularioCollectionOld = persistentPauta.getFormularioCollection();
            Collection<Formulario> formularioCollectionNew = pauta.getFormularioCollection();
            Collection<Pago> pagoCollectionOld = persistentPauta.getPagoCollection();
            Collection<Pago> pagoCollectionNew = pauta.getPagoCollection();
            List<String> illegalOrphanMessages = null;
            for (Formulario formularioCollectionOldFormulario : formularioCollectionOld) {
                if (!formularioCollectionNew.contains(formularioCollectionOldFormulario)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Formulario " + formularioCollectionOldFormulario + " since its idPauta field is not nullable.");
                }
            }
            for (Pago pagoCollectionOldPago : pagoCollectionOld) {
                if (!pagoCollectionNew.contains(pagoCollectionOldPago)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Pago " + pagoCollectionOldPago + " since its idPauta field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (loginClienteNew != null) {
                loginClienteNew = em.getReference(loginClienteNew.getClass(), loginClienteNew.getLogin());
                pauta.setLoginCliente(loginClienteNew);
            }
            if (idEsquemaNew != null) {
                idEsquemaNew = em.getReference(idEsquemaNew.getClass(), idEsquemaNew.getIdEsquema());
                pauta.setIdEsquema(idEsquemaNew);
            }
            if (idEstadoNew != null) {
                idEstadoNew = em.getReference(idEstadoNew.getClass(), idEstadoNew.getIdEstado());
                pauta.setIdEstado(idEstadoNew);
            }
            Collection<Formulario> attachedFormularioCollectionNew = new ArrayList<Formulario>();
            for (Formulario formularioCollectionNewFormularioToAttach : formularioCollectionNew) {
                formularioCollectionNewFormularioToAttach = em.getReference(formularioCollectionNewFormularioToAttach.getClass(), formularioCollectionNewFormularioToAttach.getIdFormulario());
                attachedFormularioCollectionNew.add(formularioCollectionNewFormularioToAttach);
            }
            formularioCollectionNew = attachedFormularioCollectionNew;
            pauta.setFormularioCollection(formularioCollectionNew);
            Collection<Pago> attachedPagoCollectionNew = new ArrayList<Pago>();
            for (Pago pagoCollectionNewPagoToAttach : pagoCollectionNew) {
                pagoCollectionNewPagoToAttach = em.getReference(pagoCollectionNewPagoToAttach.getClass(), pagoCollectionNewPagoToAttach.getIdPago());
                attachedPagoCollectionNew.add(pagoCollectionNewPagoToAttach);
            }
            pagoCollectionNew = attachedPagoCollectionNew;
            pauta.setPagoCollection(pagoCollectionNew);
            pauta = em.merge(pauta);
            if (loginClienteOld != null && !loginClienteOld.equals(loginClienteNew)) {
                loginClienteOld.getPautaCollection().remove(pauta);
                loginClienteOld = em.merge(loginClienteOld);
            }
            if (loginClienteNew != null && !loginClienteNew.equals(loginClienteOld)) {
                loginClienteNew.getPautaCollection().add(pauta);
                loginClienteNew = em.merge(loginClienteNew);
            }
            if (idEsquemaOld != null && !idEsquemaOld.equals(idEsquemaNew)) {
                idEsquemaOld.getPautaCollection().remove(pauta);
                idEsquemaOld = em.merge(idEsquemaOld);
            }
            if (idEsquemaNew != null && !idEsquemaNew.equals(idEsquemaOld)) {
                idEsquemaNew.getPautaCollection().add(pauta);
                idEsquemaNew = em.merge(idEsquemaNew);
            }
            if (idEstadoOld != null && !idEstadoOld.equals(idEstadoNew)) {
                idEstadoOld.getPautaCollection().remove(pauta);
                idEstadoOld = em.merge(idEstadoOld);
            }
            if (idEstadoNew != null && !idEstadoNew.equals(idEstadoOld)) {
                idEstadoNew.getPautaCollection().add(pauta);
                idEstadoNew = em.merge(idEstadoNew);
            }
            for (Formulario formularioCollectionNewFormulario : formularioCollectionNew) {
                if (!formularioCollectionOld.contains(formularioCollectionNewFormulario)) {
                    Pauta oldIdPautaOfFormularioCollectionNewFormulario = formularioCollectionNewFormulario.getIdPauta();
                    formularioCollectionNewFormulario.setIdPauta(pauta);
                    formularioCollectionNewFormulario = em.merge(formularioCollectionNewFormulario);
                    if (oldIdPautaOfFormularioCollectionNewFormulario != null && !oldIdPautaOfFormularioCollectionNewFormulario.equals(pauta)) {
                        oldIdPautaOfFormularioCollectionNewFormulario.getFormularioCollection().remove(formularioCollectionNewFormulario);
                        oldIdPautaOfFormularioCollectionNewFormulario = em.merge(oldIdPautaOfFormularioCollectionNewFormulario);
                    }
                }
            }
            for (Pago pagoCollectionNewPago : pagoCollectionNew) {
                if (!pagoCollectionOld.contains(pagoCollectionNewPago)) {
                    Pauta oldIdPautaOfPagoCollectionNewPago = pagoCollectionNewPago.getIdPauta();
                    pagoCollectionNewPago.setIdPauta(pauta);
                    pagoCollectionNewPago = em.merge(pagoCollectionNewPago);
                    if (oldIdPautaOfPagoCollectionNewPago != null && !oldIdPautaOfPagoCollectionNewPago.equals(pauta)) {
                        oldIdPautaOfPagoCollectionNewPago.getPagoCollection().remove(pagoCollectionNewPago);
                        oldIdPautaOfPagoCollectionNewPago = em.merge(oldIdPautaOfPagoCollectionNewPago);
                    }
                }
            }
            //em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = pauta.getIdPauta();
                if (findPauta(id, em) == null) {
                    throw new NonexistentEntityException("The pauta with id " + id + " no longer exists.");
                }
            }
            throw ex;
        }
    }

    /*public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Pauta pauta;
            try {
                pauta = em.getReference(Pauta.class, id);
                pauta.getIdPauta();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The pauta with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Formulario> formularioCollectionOrphanCheck = pauta.getFormularioCollection();
            for (Formulario formularioCollectionOrphanCheckFormulario : formularioCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Pauta (" + pauta + ") cannot be destroyed since the Formulario " + formularioCollectionOrphanCheckFormulario + " in its formularioCollection field has a non-nullable idPauta field.");
            }
            Collection<Pago> pagoCollectionOrphanCheck = pauta.getPagoCollection();
            for (Pago pagoCollectionOrphanCheckPago : pagoCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Pauta (" + pauta + ") cannot be destroyed since the Pago " + pagoCollectionOrphanCheckPago + " in its pagoCollection field has a non-nullable idPauta field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Cliente loginCliente = pauta.getLoginCliente();
            if (loginCliente != null) {
                loginCliente.getPautaCollection().remove(pauta);
                loginCliente = em.merge(loginCliente);
            }
            Esquema idEsquema = pauta.getIdEsquema();
            if (idEsquema != null) {
                idEsquema.getPautaCollection().remove(pauta);
                idEsquema = em.merge(idEsquema);
            }
            Estado idEstado = pauta.getIdEstado();
            if (idEstado != null) {
                idEstado.getPautaCollection().remove(pauta);
                idEstado = em.merge(idEstado);
            }
            em.remove(pauta);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }*/

    public void destroy(Integer id, EntityManager em) throws IllegalOrphanException, NonexistentEntityException, Exception {
        //EntityManager em = null;
        try {
            //em = getEntityManager();
            //em.getTransaction().begin();
            Pauta pauta;
            try {
                pauta = em.getReference(Pauta.class, id);
                pauta.getIdPauta();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The pauta with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Formulario> formularioCollectionOrphanCheck = pauta.getFormularioCollection();
            for (Formulario formularioCollectionOrphanCheckFormulario : formularioCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Pauta (" + pauta + ") cannot be destroyed since the Formulario " + formularioCollectionOrphanCheckFormulario + " in its formularioCollection field has a non-nullable idPauta field.");
            }
            Collection<Pago> pagoCollectionOrphanCheck = pauta.getPagoCollection();
            for (Pago pagoCollectionOrphanCheckPago : pagoCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Pauta (" + pauta + ") cannot be destroyed since the Pago " + pagoCollectionOrphanCheckPago + " in its pagoCollection field has a non-nullable idPauta field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Cliente loginCliente = pauta.getLoginCliente();
            if (loginCliente != null) {
                loginCliente.getPautaCollection().remove(pauta);
                loginCliente = em.merge(loginCliente);
            }
            Esquema idEsquema = pauta.getIdEsquema();
            if (idEsquema != null) {
                idEsquema.getPautaCollection().remove(pauta);
                idEsquema = em.merge(idEsquema);
            }
            Estado idEstado = pauta.getIdEstado();
            if (idEstado != null) {
                idEstado.getPautaCollection().remove(pauta);
                idEstado = em.merge(idEstado);
            }
            em.remove(pauta);
            //em.getTransaction().commit();
        } catch(Exception e) {
            throw e;
        }
    }

    /*public List<Pauta> findPautaEntities() {
        return findPautaEntities(true, -1, -1);
    }*/

    public List<Pauta> findPautaEntities(EntityManager em) throws Exception {
        return findPautaEntities(true, -1, -1, em);
    }

    /*public List<Pauta> findPautaEntities(int maxResults, int firstResult) {
        return findPautaEntities(false, maxResults, firstResult);
    }*/

    public List<Pauta> findPautaEntities(int maxResults, int firstResult, EntityManager em) throws Exception {
        return findPautaEntities(false, maxResults, firstResult, em);
    }

    /*private List<Pauta> findPautaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Pauta as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }*/

    private List<Pauta> findPautaEntities(boolean all, int maxResults, int firstResult, EntityManager em) throws Exception {
        //EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Pauta as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } catch(Exception e) {
            throw e;
        }
    }

    /*public Pauta findPauta(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Pauta.class, id);
        } finally {
            em.close();
        }
    }*/

    public Pauta findPauta(Integer id, EntityManager em) throws Exception {
        //EntityManager em = getEntityManager();
        try {
            return em.find(Pauta.class, id);
        } catch(Exception e) {
            throw e;
        }
    }

    /*public int getPautaCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Pauta as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }*/

    public int getPautaCount(EntityManager em) throws Exception {
        //EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Pauta as o");
            return ((Long) q.getSingleResult()).intValue();
        } catch(Exception e) {
            throw e;
        }
    }   

}
